<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-button type="primary" @click="addrole">添加角色</el-button>
      </div>
      <el-table
        border
        :data="tableData"
        style="width: 100%"
      >
        <el-table-column
          prop="title"
          label="角色名称"
        />
        <el-table-column
          prop="desc"
          label="角色描述"
        />
        <el-table-column
          min-width="150px"
          label="操作"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="handleEdit(scope.$index, scope.row)"
            >编辑
            </el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
            >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="50%"
    >

      <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="100px" class="demo-ruleForm">
        <el-form-item label="角色名称" prop="title">
          <el-input v-model="ruleForm.title"/>
        </el-form-item>
        <el-form-item label="角色描述" prop="desc">
          <el-input v-model="ruleForm.desc"/>
        </el-form-item>
        <el-form-item label="活动名称">
          <el-tree
            ref="tree"
            :data="data"
            :default-checked-keys="checkedKeys"
            show-checkbox
            node-key="path"
            highlight-current
            :props="defaultProps"
          />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="onsubmit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>

import {getRoleList, addRole, editRole, delRole} from './api/index'
import {asyncRoutes} from '@/router/index'

export default {
  data() {
    return {
      checkedKeys: [],
      tableData: [],
      dialogVisible: false,
      data: [],
      isApit: '',
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      ruleForm: {
        title: '',
        desc: '',
        role: []
      },
      rules: {
        title: [
          {required: true, message: '请输入角色名称', trigger: 'blur'}
        ],
        desc: [
          {required: true, message: '请输入角色名称', trigger: 'blur'}
        ]
      }
    }
  },
  created() {
    this.getRole()
    this.data = asyncRoutes
  },
  methods: {
    getRole() {
      getRoleList().then(x => {
        this.tableData = x.data
        console.log(x)
      })
    },
    onsubmit() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          if (this.isApit == '1') {
            this.ruleForm.role = this.$refs.tree.getCheckedKeys().concat(this.$refs.tree.getHalfCheckedKeys()).join(',')
            this.dialogVisible = false
            addRole(this.ruleForm).then(x => {
              if (x.code == '20000') {
                this.$message({type: 'success', message: '添加用户成功'})
                this.getRole()
              } else {
                this.$message({type: 'error', message: '添加用户失败'})
              }
            })
            console.log('添加')
          } else if (this.isApit == '2') {
            this.ruleForm.role = this.$refs.tree.getCheckedKeys().concat(this.$refs.tree.getHalfCheckedKeys()).join(',')
            this.dialogVisible = false
            editRole(this.ruleForm).then(x => {
              if (x.code == '20000') {
                this.$message({type: 'success', message: '添加用户成功'})
                this.getRole()
              } else {
                this.$message({type: 'error', message: '添加用户失败'})
              }
            })
            console.log('修改')
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    addrole() {
      this.isApit = '1'
      this.dialogVisible = true
      this.$nextTick(() => {
        this.ruleForm = {...''}
        this.$refs.tree.setCheckedKeys([])
      })
    },
    handleEdit(index, row) {
      if (row.id == '1') {
        this.$message({type: 'success', message: '源数据不能删除'})
        return
      }
      var str = row.role.split(',')
      this.data.forEach(x => {
        for (let i = 0; i < str.length; i++) {
          console.log(x.meta.roles, str[i])
          if (x.meta.roles == str[i]) {
            console.log('1111')
            str.splice(i, 1)
          }
        }
      })
      console.log(str)
      this.isApit = '2'
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs.tree.setCheckedKeys([])
        this.checkedKeys = str
        this.ruleForm = {
          title: row.title,
          desc: row.desc,
        }
        this.ruleForm.id = row.id
      })
    },
    handleDelete(index, row) {
      if (row.id == '1') {
        this.$message({type: 'success', message: '源数据不能删除'})
        return
      }
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {

        delRole({id: row.id}).then(x => {
          if (x.code == '20000') {
            this.$message({type: 'success', message: '删除用户成功'})
            this.getRole()
          } else {
            this.$message({type: 'error', message: '删除用户失败'})
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })

      console.log(index, row)
    }
  }
}
</script>

<style scoped>

</style>
